Tesselator = class()

function Tesselator:init()
    self.mesh = mesh()
    self.mesh.texture = Tex
    self:clear()
end

function Tesselator:setShader(Shader)
    self.shader = Shader
end

function Tesselator:clear()
    -- self.mesh:clear()
    self.vertexBuffer = {}
    self.texCoordBuffer = {}
    self.colorBuffer = {}
    self.hasTexture = false
    self.hasColor = true
    self.noColor = false
    self.point = 1
    self.r,self.g,self.b,self.a = 0,0,0,0
    self.u,self.v = 0,0
    self.alpha = 255
end

function Tesselator:render()
    if self.point > 1 then
        self.mesh:draw()
    end
end

function Tesselator:begin()
    self:clear()
end

--[[
function Tesselator:overWrite(index)
    -- self.point = index
    local tex = self.mesh.texture
    self:begin()
    self:bindTex(tex)
end
  ]]

function Tesselator:noColor()
    self.hasColor = false
end

function Tesselator:End()
    self.mesh:clear()
    self.mesh.vertices = self.vertexBuffer
    self.vertexBuffer = nil
    if self.hasTexture then
        self.mesh.texCoords = self.texCoordBuffer
        self.texCoordBuffer = nil
    end
    if self.hasColor then
        self.mesh.colors = self.colorBuffer
        self.colorBuffer = nil
    else
        self.mesh:setColors(255,255,255,255)
    end
    
    self.mesh.shader = self.shader
end

function Tesselator:color3b(r,g,b)
    self:color4b(r,g,b,255)
end

function Tesselator:color4b(r,g,b,a)
    self.r,self.g,self.b,self.a = r,g,b,a
end

function Tesselator:color3f(r,g,b)
    self:color4b(r*255,g*255,b*255,255)
end

function Tesselator:color4f(r,g,b,a)
    self:color4b(r*255,g*255,b*255,a*255)
end

function Tesselator:brightf(b)
    self:color4b(b*255,b*255,b*255,255)
end

function Tesselator:brightb(b)
    self:color4b(b,b,b,255)
end

function Tesselator:brightbWithAlpha(b,a)
    self:color4b(b,b,b,a)
end

function Tesselator:alphab(a)
    self.alpha = a
end

function Tesselator:bindTex(tex)
    self.mesh.texture = tex
    self.hasTexture = true
end

function Tesselator:tex(u,v)
    self.u,self.v = u,v
end

function Tesselator:vertex(x,y,z)
    self.vertexBuffer[self.point] = vec3(x,y,z)
    if self.hasTexture then
        self.texCoordBuffer[self.point] = vec2(self.u,self.v)
    end
    if self.hasColor then
        self.colorBuffer[self.point] = color(self.r,self.g,self.b,self.a)
    end
    self.point = self.point + 1
end

function Tesselator:vertexUV(x,y,z,u,v)
    self.vertexBuffer[self.point] = vec3(x,y,z)
    self.texCoordBuffer[self.point] = vec2(u,v)
    self.colorBuffer[self.point] = color(self.r,self.g,self.b,self.a)
    self.point = self.point + 1
end

function Tesselator:vertexUVC(x,y,z,u,v,c)
    self.vertexBuffer[self.point] = vec3(x,y,z)
    self.texCoordBuffer[self.point] = vec2(u,v)
    self.colorBuffer[self.point] = color(c)
    self.point = self.point + 1
end

function Tesselator:vertexUVCA(x,y,z,u,v,c)
    self.vertexBuffer[self.point] = vec3(x,y,z)
    self.texCoordBuffer[self.point] = vec2(u,v)
    self.colorBuffer[self.point] = color(c,self.alpha)
    self.point = self.point + 1
end